<?php

namespace Lib\View\Helper;

use Lib\View\Helper\Helper;

class Html extends Helper {
    
    public function image($file, array $options = array()) {
        
        $attrs = array(
            'src' => "/img/{$file}"
        );

        // extract link
        $link = @$options['link'];
        unset($options['link']);

        // prepare HTML attrs
        $attrs =  array_merge($attrs, $options);
        $attributes = "";
        foreach ($attrs as $name=>$value) {
            $attributes .= " {$name}=\"{$value}\"";
        }

        // prepare link
        $linkStart = $linkEnd = '';
        if (isset($link)) {
            $linkAttrs = '';
            if (is_array($link)) {
                $href = $link['href'];
                unset($link['href']);
                foreach ($link as $key=>$val) {
                    $linkAttrs .= " $key=\"{$val}\"";
                }
            } else {
                $href = $link;
            }

            $linkStart = '<a href="' . $href . '"' . $linkAttrs . '>';
            $linkEnd   = '</a>';
        }

        return $linkStart . "<img{$attributes}>" . $linkEnd;
    }
}